package com.kkw.shoppingmall.search.controller;

import com.kkw.shoppingmall.search.service.MallSearchService;
import com.kkw.shoppingmall.search.vo.SearchParam;
import com.kkw.shoppingmall.search.vo.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kevin
 * @date 2021-06-06
 **/
@Controller
@Slf4j
public class SearchController {

    @Autowired
    private MallSearchService mallSearchService;

    /**
     *
     * @param searchParam 检索的参数
     * @return
     */
    @GetMapping("list.html")
    public String listPage(SearchParam searchParam, Model model, HttpServletRequest request){
        //拿到后面的完整的查询字符串
        String queryString = request.getQueryString();
        searchParam.set_queryString(queryString);
        //1、根据传递过来的参数，去es中检索商品
        try {
            SearchResult result=mallSearchService.search(searchParam);
            model.addAttribute("result",result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "list";
    }
}
